/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.base.bean;

import com.ibatis.dao.client.DaoManager;
import com.yunhoo.base.model.Department;
import com.yunhoo.base.model.Person;
import com.yunhoo.sys.db.config.DaoConfig;
import com.yunhoo.base.db.dao.PersonDao;
import com.yunhoo.base.db.impl.PersonImpl;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.ToolKit;
import com.yunhoo.sys.util.tree.TreeUtil;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;
import org.richfaces.event.TreeSelectionChangeEvent;

@ManagedBean(name = "personBean")
@SessionScoped
public class PersonBean implements Serializable {

    private static DaoManager daom;
    private static PersonDao dao;
    //
    private Department dept = null;
    private List<Person> personlist = null;
    private ListDataModel<Person> personModel = null;
    private Person person;
    private boolean addOrUpdate = false;
    private boolean editStatus = false;
    //
    private List<Person> perIdReferList = null;//工资数据库中，人员id和名称 对照表
    private String nameFilter = "";//名称过滤

    public PersonBean() {
        daom = DaoConfig.getDaoManager();
        dao = (PersonDao) daom.getDao(PersonDao.class);

        person = new Person();
        person.setOrgCode(LoginParam.getOrg().getCode());
        personModel = new ListDataModel<Person>();
    }

    private void initStatus() {
        addOrUpdate = false;
        editStatus = false;
    }

    public void add() {
        if (dept == null) {
            ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "请选择部门！");
            return;
        }

        editStatus = true;
        addOrUpdate = true;

        person = new Person();
        person.setOrgCode(LoginParam.getOrg().getCode());
        person.setDeptCode(dept.getCode());
        person.setDeptName(dept.getName());
        person.setCpsn_number(buildCode());
    }

    private String buildCode() {
        String deptCode = dept.getCode();
        Integer maxCode = dao.maxCode(deptCode);
        if (maxCode == -1) return null;
        if (maxCode == null) {
            maxCode = 0;
        }
        return deptCode + String.format("%03d%n", maxCode + 1).trim();
    }

    public void update() {
        addOrUpdate = false;
        editStatus = true;
    }

    public void delete() {
        boolean ok = true;
        try {
            dao.delete(person);
        } catch (SQLException ex) {
            ok = false;
            Logger.getLogger(PersonBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (ok) {
            person = null;
            initStatus();
        }
    }

    public void save() {
        boolean ok = true;
        try {
            daom.startTransaction();
            if (addOrUpdate) {
                if (dao.numberIsRepeat(person.getCpsn_number())) {
                    //id重复
                    ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "人员代码已存在！");
                    return;
                }
                if (dao.nameIsRepeate_add(person.getCpsn_number())) {
                    //名称重复
                    ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "名称已存在！");
                    return;
                }

                person.setId(dao.maxId() + 1);
                dao.insert(person);
            } else {
                if (dao.nameIsRepeate_update(person)) {
                    //名称重复
                    ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "名称已存在！");
                    return;
                }
                dao.update(person);
            }
            daom.commitTransaction();
        } catch (SQLException ex) {
            ok = false;
            daom.endTransaction();
            Logger.getLogger(PersonBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (ok) {
            initStatus();
        }
    }

    public void cancel() {
        if (addOrUpdate) {
            person = null;
        }
        initStatus();
    }

    public void personSelected() {
        person = personModel.getRowData();
        initStatus();
    }

    public void treeSelected(TreeSelectionChangeEvent event) {
        dept = (Department) TreeUtil.listener(event);
        String deptcode = dept.getCode().trim();
        Person param = new Person();
        param.setOrgCode(LoginParam.getOrg().getCode());
        param.setIyear(LoginParam.getIyear());
        param.setDeptCode(deptcode);
        try {
            personlist = dao.queryListByDept(param);
        } catch (SQLException ex) {
            Logger.getLogger(PersonBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        personModel.setWrappedData(personlist);
        initStatus();
    }

    public List<Person> getPersonlist() {
        return personlist;
    }

    public void setPersonlist(List<Person> personlist) {
        this.personlist = personlist;
    }

    public ListDataModel<Person> getPersonModel() {
        return personModel;
    }

    public void setPersonModel(ListDataModel<Person> personModel) {
        this.personModel = personModel;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public boolean isEditStatus() {
        return editStatus;
    }

    public void setEditStatus(boolean editStatus) {
        this.editStatus = editStatus;
    }

    public String getNameFilter() {
        return nameFilter;
    }

    public void setNameFilter(String nameFilter) {
        this.nameFilter = nameFilter;
    }

    public List<Person> getPerIdReferList() {
        if (perIdReferList == null) {
            initPerIdReferList();
        }
        return perIdReferList;
    }

    private void initPerIdReferList() {
        perIdReferList = PersonImpl.queryPerIdReferList();
    }
}
